package com.uestc.servlet.statistics;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.uestc.admin.UserAdmin;
import com.uestc.admin.UserFeedBackAdmin;
import com.uestc.hbase.MTable;
import com.uestc.servlet.ServletBase;
import com.uestc.util.Log;
import com.uestc.util.SysProperties;

@WebServlet("/admin/userstatistics")
public class UserStatistics extends HttpServlet implements ServletBase {
	private static final long serialVersionUID = 1L;

	public UserStatistics() {
		super();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String param = getParameter(request, "adminOpt");
		if ("totalUser".equals(param)) {
			Log.logger.info("收到查询用户总数的请求");
			MTable mTable = new MTable("user");
			long count = 0;
			try {
				count = mTable.getRowCounter("user", "info", "create_time");
				writeMessage(response, String.valueOf(count));
				return;
			} catch (Throwable e) {
				Log.logException(e);
				return;
			} finally {
				mTable.close();
			}
		} else if ("registerLocation".equals(param)) {
			UserAdmin ua = new UserAdmin();
			try {
				writeMessage(response, ua.getRegisterUserDistribution());
			} catch (Throwable e) {
				Log.logException(e);
			}
		} else if ("activeUserLocation".equals(param)) {
			String beginDate = getParameter(request, "beginDate");
			String endDate = getParameter(request, "endDate");
			UserAdmin ua = new UserAdmin();
			try {
				writeMessage(response, ua.getActiveUserDistribution(beginDate, endDate));
			} catch (Throwable e) {
				Log.logException(e);
			}
		} else if ("registerQuantity".equals(param)) {
			String beginDate = getParameter(request, "beginDate");
			String endDate = getParameter(request, "endDate");
			UserAdmin ua = new UserAdmin();
			try {
				writeMessage(response, ua.getNewRegistrationNumber(beginDate, endDate));
			} catch (Throwable e) {
				Log.logException(e);
			}
		} else if ("feedbackStatistc".equals(param)) {
			String sheet = getParameter(request, "sheet");
			String colunms = SysProperties.getValue(sheet);
			String type = sheet + ";" + colunms;
			UserFeedBackAdmin ufba = new UserFeedBackAdmin();
			String result = ufba.getFeedbackStatistics(type);
			writeMessage(response, result);
		} else if ("clear_used_flow".equals(param)) {
			UserAdmin ua = new UserAdmin();
			ua.recoverUsedFlow();
			writeMessage(response, "success");
		}

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
